﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SVAP
{
    public partial class ConverterForm : Form
    {
        public ConverterForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog Open_file = new OpenFileDialog();
            if (Open_file.ShowDialog() == DialogResult.OK)
            {
                //Open_file.Title = "请选择文件";
                textBox1.Text = Open_file.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog Save_file = new SaveFileDialog();
            Save_file.Filter = "avi文件|*.avi|flv文件|*.flv|mpg文件|*.mpg|rm文件|*.rm|所有文件|*.*";
            //Save_file.Title = "请选择文件存储位置及文件名";
            if (Save_file.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = Save_file.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Length == 0)
            {
                MessageBox.Show("文件路径不能为空!");
                return;
            }
            if (textBox2.Text.Trim().Length == 0)
            {
                MessageBox.Show("文件存储路径不能为空!");
                return;
            }
            if (Path.GetExtension(textBox1.Text) == Path.GetExtension(textBox2.Text))
            {
                MessageBox.Show("文件格式相同，不用格式转换！");
                return;
            }
            if (Path.GetExtension(textBox2.Text) == System.String.Empty)
            {
                MessageBox.Show("请输入文件的后缀名！");
                return;
            }

            System.Diagnostics.Process p = new System.Diagnostics.Process();//建立外部调用线程
            p.StartInfo.FileName = Application.StartupPath + "\\ffmpeg.exe";
            p.StartInfo.Arguments = "-y -i " + textBox1.Text + " -sameq " + textBox2.Text;//FFMPEG的参数
            p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的.mencoder用standardOutput来捕获)
            p.StartInfo.CreateNoWindow = true;//不创建进程窗口
            //p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
            p.Start();//启动线程
            p.BeginErrorReadLine();//开始异步读取
            p.WaitForExit();//阻塞等待进程结束
            p.Close();//关闭进程
            p.Dispose();//释放资源

            FileInfo f = new FileInfo(textBox2.Text);
            if (!f.Exists || f.Length == 0)
            {
                MessageBox.Show("转换失败！");
            }
            else if (f.Exists && f.Length > 0)
            {
                MessageBox.Show("转换成功！");
            }
            this.Close();
        }
    }
}